//mutation 文件
import Vue from 'vue'
import  {
    RECIVE_ADDRESS,
    RECIVE_CATEGORYS,
    RECIVE_SHOPS,
    RECIVE_USER_INFO,
    RESET_USER_INFO,
    RECIVE_GOODS,
    RECIVE_INFO,
    RECIVE_RATINGS,
    INCREMENT_FOOD_COUNT,
    DECREMENT_FOOD_COUNT,
    CLEAR_CART,
    RECIVE_SEARCH_SHOPS

} from './mutation-types.js'
export default {
    [RECIVE_ADDRESS](state,{address}){ //地址数据
      state.address=address
    },
    [RECIVE_CATEGORYS](state,{categorys}){ //食品分类数据
        state.categorys=categorys
    },
    [RECIVE_SHOPS](state,{shops}){  //店铺列表
        state.shops=shops
    },

    [RECIVE_USER_INFO](state,{userInfo}){ //接收用户信息
            state.userInfo=userInfo
    },
    [RESET_USER_INFO](state,{}){ //重置，清空用户信息
        state.userInfo={}
    },
    [RECIVE_INFO](state,{info}){ //店铺详情信息
        state.info=info
    },
    [RECIVE_GOODS](state,{goods}){ //店铺详细食品信息
        state.goods=goods
    },
    [RECIVE_RATINGS](state,{ratings}){//店铺评论
        state.ratings=ratings
    },
    [INCREMENT_FOOD_COUNT](state,{food}){ //增加购物车控制小组件，数量+1
        if(!food.count){
            //food.count=0;数据更新但是不显示
            Vue.set(food,'count',1);
            state.cartFoods.push(food);

            sessionStorage.setItem("defaultCartFoods",JSON.stringify(state.cartFoods))
        }else{
            food.count++;
            sessionStorage.setItem("defaultCartFoods",JSON.stringify(state.cartFoods))
        }
    },
    [DECREMENT_FOOD_COUNT](state,{food}){//减少购物车小组件,数量-1
        if(food.count){//防止出现负数还在减少
            food.count--;
            if(food.count===0){
                state.cartFoods.splice(state.cartFoods.indexOf(food),1);
            }
            sessionStorage.setItem("defaultCartFoods",JSON.stringify(state.cartFoods))
        }
    },
    [CLEAR_CART](state){
        state.cartFoods.forEach((food,index)=>{  //先把有count的food清零
            food.count=0
        });
        //清空购物车，这里有改变指向问题，改变堆的指向，但是里面各个food还在，count还在。
        state.cartFoods=[];
        sessionStorage.setItem("defaultCartFoods",JSON.stringify(state.cartFoods))
    },
    [RECIVE_SEARCH_SHOPS](state,{searchShops}){
        state.searchShops=searchShops;
    }
}
